package cn.glutnn.service;

import java.util.ArrayList;
import java.util.List;

import cn.glutnn.dao.RoomDao;
import cn.glutnn.dao.SeatDao;
import cn.glutnn.entity.Room;
import cn.glutnn.entity.Seat;

public class RoomService {
	RoomDao roomDao = new RoomDao();
	SeatDao seatDao = new SeatDao();

	public boolean saveRoomAndSeat(Room room) {
		// 自动生成座位信息，座位编号规则为“1-1,1-2,1-3,...,2-1,2-2”
		// 每十个座位为一行
		boolean isSaveRoomOK = roomDao.save(room);

		Integer num = room.seatnum;

		List<Seat> list = new ArrayList<>();

		for (int i = 0, ii = num; i < ii; i++) {
			// 构造编码

			// 行
			Integer row = i / 10 + 1;
			// 列
			Integer col = i % 10 + 1;

			String code = row + "-" + col;

			Seat seat = new Seat(room.id, code, Boolean.FALSE);

			list.add(seat);
		}

		// 5、分别调用RoomDao,SeatDao，保存影厅及座位信息


		boolean isSaveSeatOK = seatDao.save(list);

		return isSaveRoomOK && isSaveSeatOK;
	}

	public boolean updateRoomAndSeat(Room room) {
		// TODO: 完善保存后的判断逻辑
		
		
		Integer seatnum = room.seatnum;

		if (seatnum.intValue() != 0) {
			// 先删除，后新增
			List<Seat> seatDeleteList = seatDao.findByRoomid(room.id);
			for (Seat seat : seatDeleteList) {
				boolean isDeleteOK = seatDao.delete(seat.id);
			}

			List<Seat> seatSaveList = new ArrayList<>();

			for (int i = 0, ii = seatnum; i < ii; i++) {
				// 构造编码

				// 行
				Integer row = i / 10 + 1;
				// 列
				Integer col = i % 10 + 1;

				String code = row + "-" + col;

				Seat seat = new Seat(room.id, code, Boolean.FALSE);

				seatSaveList.add(seat);
			}

			boolean isSaveSeatOK = seatDao.save(seatSaveList);
		}

		return true;
	}
}
